package com.example.hotel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.hotel.R.drawable;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Message_list extends Activity implements OnClickListener {
	public ArrayList<String> message = new ArrayList<String>();
	HashMap<String, String> planetList1;
	SimpleAdapter sAdap;
	ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
	TextView history, date, wellcome, tv_message;
	ImageView back_home, imageback, image_cart, image_msg, image_bill;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_list);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		TextView tvAmount = (TextView) findViewById(R.id.tvAmount);
		tvAmount.setText("("
				+ ((Global) getApplicationContext()).cart_count.toString()
				+ ")");
		getMessage_list();
		ListView list_message = (ListView) findViewById(R.id.list_message);
		ListAdapter listAdapter;
		listAdapter = new CustomListAdapter(Message_list.this,
				R.layout.message_column, message);
		list_message.setAdapter(listAdapter);
		history = (TextView) findViewById(R.id.tv_history);
		date = (TextView) findViewById(R.id.date_message);
		// head_message=(TextView)findViewById(R.id.message_list);
		back_home = (ImageView) findViewById(R.id.imagehome);
		imageback = (ImageView) findViewById(R.id.imageback);
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		tv_message = (TextView) findViewById(R.id.Message);
		wellcome.setText(((Global) getApplicationContext()).wellcome);
		back_home.setOnClickListener(this);
		Boolean flag = ((Global)getApplicationContext()).callpage_bool;
		if(flag)
		{
			imageback.setImageDrawable(getResources().getDrawable(R.drawable.arrow_upright));
			imageback.setOnClickListener(this);						
		}
		else
		{			
			((Global)getApplicationContext()).callpage_bool = false;
		}
		
		image_cart.setOnClickListener(this);
		image_msg.setOnClickListener(this);
		image_bill.setOnClickListener(this);

		/*------setfonts------*/
		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		history.setTypeface(FontBold);
		date.setTypeface(FontBold);
		// head_message.setTypeface(FontBold);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		tv_message.setTypeface(FontBold);
		/*--------------------*/
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imagehome:
			((Global)getApplicationContext()).callpage_bool = false;
			startActivity(new Intent(Message_list.this, home.class));
			break;
		case R.id.imageback:
			Class myclass = ((Global)getApplicationContext()).callpage1;
			 startActivity(new Intent(Message_list.this, myclass));
			break;			
		case R.id.image_cart:
			((Global) getApplicationContext()).nameCart = "show_cart";
			startActivity(new Intent(Message_list.this, Add_cart.class));
			break;
		case R.id.image_msg:
			startActivity(new Intent(Message_list.this, Message_list.class));
			break;
		case R.id.image_bill:
			startActivity(new Intent(Message_list.this, Billing_history.class));
			break;
		}
	}

	// /CustomList setFontListView
	@SuppressWarnings("rawtypes")
	private class CustomListAdapter extends ArrayAdapter {

		private Context mContext;
		private int id;
		private List<String> items;

		@SuppressWarnings("unchecked")
		public CustomListAdapter(Context context, int textViewResourceId,
				List<String> list) {
			super(context, textViewResourceId, list);
			mContext = context;
			id = textViewResourceId;
			items = list;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			View mView = v;
			if (mView == null) {
				LayoutInflater vi = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				mView = vi.inflate(id, null);
			}
			TextView text = (TextView) mView.findViewById(R.id.message_list);
			if (items.get(position) != null) {
				Typeface Font = Typeface.createFromAsset(getAssets(),
						"fonts/PSL-Kittithada.ttf");
				text.setTypeface(Font);
				text.setText(items.get(position));
			}
			return mView;
		}

	}

	public void getMessage_list() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).url
				+ "message_list";
		String NAMESPACE = "";
		String METHOD_NAME = "message_list";
		String lang = ((Global) getApplicationContext()).lang;
		String device_id = ((Global) getApplicationContext()).imei;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", device_id);
		request.addProperty("lang", lang);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			String[] a = b.split("\\|");
			indexSub = a[a.length - 1].indexOf("}");
			b = a[a.length - 1].substring(0, indexSub - 2);
			a[a.length - 1] = b;
			for (int i = 0; i < a.length; i++) {
				String[] tmp = a[i].split("\\*");
				String str1 = String.valueOf(Html.fromHtml(tmp[1]));
				message.add(tmp[0] + "     " + str1);

			}

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}



}
